package main

import (
	"flag"
	"fmt"
	"os"

	"service/internal/config"
	ClassificationServer "service/internal/server/Classification"
	GoodsServer "service/internal/server/Goods"
	GoodsTemplateServer "service/internal/server/goodsTemplate"
	ShopsServer "service/internal/server/shops"
	sumServer "service/internal/server/sum"
	testrpcServer "service/internal/server/testrpc"
	UsersServer "service/internal/server/users"
	"service/internal/svc"
	"service/pb"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

// var configFile = flag.String("f", "etc/admin-uat.yaml", "the config file")
var env string

func main() {
	flag.Parse()

	if envEnv := os.Getenv("GO_ENV"); envEnv != "" {
		env = envEnv
	}

	if env == "" {
		env = "dev" // 默认使用开发环境
	}

	var configPath string
	switch env {
	case "dev":
		configPath = "etc/admin.yaml"
	case "uat":
		configPath = "etc/admin-uat.yaml"
	case "prd":
		configPath = "etc/admin-prd.yaml"
	default:
		fmt.Println("Unknown environment:", env)
		return
	}

	fmt.Println("configPat:", configPath)
	var configFile = flag.String("f", configPath, "the config file")

	var c config.Config
	conf.MustLoad(*configFile, &c)
	ctx := svc.NewServiceContext(c)

	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		pb.RegisterSumServer(grpcServer, sumServer.NewSumServer(ctx))
		pb.RegisterTestRpcServer(grpcServer, testrpcServer.NewTestRpcServer(ctx))
		pb.RegisterClassificationServer(grpcServer, ClassificationServer.NewClassificationServer(ctx))
		pb.RegisterGoodsServer(grpcServer, GoodsServer.NewGoodsServer(ctx))
		pb.RegisterUsersServer(grpcServer, UsersServer.NewUsersServer(ctx))
		pb.RegisterShopsServer(grpcServer, ShopsServer.NewShopsServer(ctx))
		pb.RegisterGoodsTemplateServer(grpcServer, GoodsTemplateServer.NewGoodsTemplateServer(ctx))
		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})
	defer s.Stop()

	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
	s.Start()
}
